//
//  QFStatusOriginalView.swift
//  sinaWeibo
//
//  Created by apple on 16/3/6.
//  Copyright © 2016年 Qifeng Yan. All rights reserved.
//

import UIKit
import SDWebImage

let QFStatusCellMargin = 8.0

class QFStatusOriginalView: UIView {

    // MARK: - 属性 模型数据
    var status: QFStatus? {
        didSet{
            // 设置头像
            if let profile_image_url = status?.user?.profile_image_url {
                let url = NSURL(string: profile_image_url)
                iconView.sd_setImageWithURL(url)
            }
            
            // 设置用户昵称
            nameLabel.text = status?.user?.screen_name
            
            // 设置会员等级
            if let mbrank = status?.user?.mbrank {
                if mbrank >= 1 && mbrank <= 6 {
                    memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
                } else {
                    memberView.image = nil
                }
            }
            
            // 设置时间
            timeLable.text = status?.created_at
            
            // 设置来源
            sourceLable.text = status?.source
            
            // 设置内容
            contentLabel.text = status?.text
            
            // 设置认证等级
            if let verified_type = status?.user?.verified_type {
                var image: UIImage? = nil
                switch verified_type {
                case 0:
                    image = UIImage(named: "avatar_vip")
                case 2,3,5:
                    image = UIImage(named: "avatar_enterprise_vip")
                case 220:
                    image = UIImage(named: "avatar_grassroot")
                default:
                    image = nil
                }
                
                verifiedTypeImage.image = image
            }
        }
    }
    
    // MARK: - 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        prepareUI()
    }
    
    // MARK: - 布局子控件
    private func prepareUI() {
        // 添加子控件
        addSubview(iconView)        // 头像
        addSubview(nameLabel)       // 用户昵称
        addSubview(memberView)      // 会员等级
        addSubview(timeLable)       // 发表时间
        addSubview(sourceLable)     // 来源
        addSubview(verifiedTypeImage)   // 认证等级
        addSubview(contentLabel)    //内容
        
        // 设置约束
        // 设置头像约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(QFStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        // 设置用户昵称约束
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(QFStatusCellMargin)
        }
        
        // 设置会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(QFStatusCellMargin)
            make.size.equalTo(CGSize(width: 15, height: 15))
        }
        
        // 设置时间
        timeLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(QFStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        
        // 设置来源
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLable.snp_trailing).offset(QFStatusCellMargin)
            make.centerY.equalTo(timeLable)
        }
        
        // 文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(QFStatusCellMargin)
            make.trailing.equalTo(self).offset(-QFStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(QFStatusCellMargin)
        }
        
        // 认证等级
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
//            make.size.equalTo(CGSize(width: 15, height: 15)) 
        }
        
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentLabel).offset(QFStatusCellMargin)
        }
    }
    
    // MARK: - 懒加载子控件
    // 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    // 用户昵称
    private lazy var nameLabel: UILabel = UILabel(color: UIColor.darkGrayColor(), font: 14)
    
    // 会员等级
    private lazy var memberView: UIImageView = UIImageView(image: nil)
    
    // 发表时间
    private lazy var timeLable: UILabel = UILabel(color: UIColor.orangeColor(), font: 11)
    
    // 微博来源
    private lazy var sourceLable: UILabel = UILabel(color: UIColor.lightGrayColor(), font: 11)
    
    // 认证图片
    private lazy var verifiedTypeImage: UIImageView = UIImageView(image: nil)
    
    // 微博内容
    private lazy var contentLabel:UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), font: 16)
        
        // 换行
        label.numberOfLines = 0
    
        
        return label
    }()
}
